import { omit } from 'lodash-es';

const extendUserPlugin = (plugin) => {
  const me = async (ctx) => {
    const authUser = ctx.state.user;

    if (!authUser) {
      return ctx.unauthorized();
    }

    const target = await strapi.documents("plugin::users-permissions.user").findOne({
      documentId: authUser.documentId,
      populate: {
        'system_config': true,
        info: {
          populate: {
            avatar: true,
          }
        }
      },
    });

    const myInfo = omit(target, ['provider', 'password', 'confirmed', 'blocked']);

    return myInfo;
  }

  plugin.controllers.user.me = me;

  // 添加策略
  // plugin.policies[newPolicy] = (ctx) => {};

  // 添加路由
  // plugin.routes['content-api'].routes.push({
  //   method: 'GET',
  //   path: '/user/updateMe',
  //   handler: 'user.updateMe',
  //   config: {
  //     prefix: ""
  //   }
  // });

  return plugin;
};

export default extendUserPlugin;
